layui.use(['table', 'jquery', 'element', 'form', 'layer', 'laydate'], function () {
    var table = layui.table;
    var $ = layui.$;
    var element = layui.element;
    var laydate = layui.laydate;
    var form = layui.form;

    var newEmp = $('#newEmp');
    var empForm = $('#newEmpForm');
    var currentHref = 0;

    empForm.hide();
    table.render({
        elem: '#table'
        , url: '/emp/getWorkEmp'
        , where: {
            department: 0,
            search: 0
        }
        , id: "emp-table"
        , page: true
        , height: 512
        , limit: 10
        , cols: [[
            {field: 'username', title: '用户名'}
            , {field: 'gender', title: '性别'}
            // ,{field:'birthday',title:'出生年月'}
            // ,{field:'idCard',title:'身份证'}
            // ,{field:'area',title:'区域'}
            // ,{field:'nativePlace',title:'籍贯'}
            // ,{field:'state',title:'状态'}
            , {field: 'dName', title: '部门'}
            , {field: 'level', title: '级别'}
            // , {field: 'operation', title: '操作', fixed: 'right', width: 110, align: 'center', toolbar: '#barDemo'}
        ]]
    });

    function test() {

        $('.department').bind("click", function (e) {
            e.preventDefault();
            var href = $(this).attr("href");
            // console.log(href);
            currentHref = href;
            table.reload('emp-table', {
                where: {
                    department: href
                }
            });
        });
    }

    test();

    element.on('tab(tab)', function (data) {
        var index = data.index;
        console.log(index);
        if (index == 0) {
            location.href = '/emp';
        } else if (index == 1) {
            location.href = '/emp/department';
        } else if (index == 2) {
            location.href = '/emp/entry';
        }
        return false;
    });


    newEmp.click(function () {

        layer.open({
            title: "新增员工",
            type: 1,
            area: 'auto',
            maxWidth: 1000,
            content: $("#newEmpForm")
        });
        empForm.show();
        return false;
    });
    //

    laydate.render({
        elem: '#birthday'
    });

    form.on('submit(newEmpSubmit)', function (data) {
        $.post('/emp/newEmp', data.field, function (data) {
            if (!data.status) {
                layer.open({title: '警告', content: data.msg});
            } else {
                empForm[0].reset();
                layer.closeAll();
                layer.open({
                    title: '成功',
                    content: "新增用户成功！"
                });
                table.reload('emp-table', {
                    where: {
                        department: 0
                    }
                })
            }
        });
        return false;
    });

    form.on('submit(search)', function (data) {
        table.reload('emp-table', {
            where: {
                department: currentHref,
                search: data.field.id
            }
        });
        return false;
    });

});